package com.hidtechs.popfax.iconifiedList;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.sax.StartElementListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.hidtechs.popfax.controller.Constants;
import com.hidtechs.popfax.controller.CurrentTab;
import com.hidtechs.popfax.controller.Requestor;
import com.hidtechs.popfax.view.Login;
import com.hidtechs.popfax.xml.ParsedDeleteDataSet;

/** @author Steven Osborn - http://steven.bitsetters.com */
public class IconifiedTextListAdapter extends BaseAdapter implements Runnable  {

	/** Remember our context so we can use it when constructing views. */
	private Context mContext;
	private String fax_id;
	private ArrayList<String> itemSelected;
	private String mtab;
	String userName_shared_prefernece;
	String passWord_shared_prefernece;
	protected Handler handler;
	protected HttpURLConnection conn=null;
	protected ArrayList arrayOfObjectOfParsedData;
	public HashMap<String,String> parameterValues;
	private ProgressDialog progressDialog;
	private static String fax_Id;
	
	public static String getFax_Id() {
		return fax_Id;
	}


	public void setFax_Id(String fax_Id) {
		this.fax_Id = fax_Id;
	}

	private ArrayList<ArrayList<String>> mItems = new ArrayList();

	public IconifiedTextListAdapter(Context context,String tab) {
		mContext = context;
		mtab=new String();
		mtab = tab;
	}

	public void addItem(ArrayList<String> it) {
		mItems.add(it);
	}

	public void addButton(ArrayList<String> it) {
		mItems.add(it);
	}

	
	
	
	/** @return The number of items in the */
	public int getCount() { return mItems.size(); }

	public ArrayList<String> getItem(int position) { return mItems.get(position); }

	public boolean areAllItemsSelectable() { return false; }

	/*    public boolean isSelectable(int position) {
          try{
               return mItems.get(position).isSelectable();
          }catch (IndexOutOfBoundsException aioobe){
               return super.isEnabled(position);
          }
     }*/

	/** Use the array index as a unique id. */
	public long getItemId(int position) {
		return position;
	}

	/** @param convertView The old view to overwrite, if one is passed
	 * @returns a IconifiedTextView that holds wraps around an IconifiedText */
	public View getView(final int position, View convertView, ViewGroup parent) {
		IconifiedTextView btv;
		if (convertView == null) {
			btv = new IconifiedTextView(mContext, mItems.get(position));
			
			 if (position % 2 == 0 )
			{
				btv.setBackgroundColor(0xffdddddd);				
			}else
			{
				btv.setBackgroundColor(Color.WHITE);
			}	
			 btv.setScrollContainer(true);
			 btv.setFocusableInTouchMode(true);
			 
			 if(position !=0)//not allow to click on header row
			 btv.setOnClickListener(new OnClickListener() {
					
					 public void onClick(View view) {
			
						 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
						 builder.setCancelable(true);
						
						 builder.setTitle(Constants.ALERTDIALOG_DATA);
						 builder.setInverseBackgroundForced(true);
						 
						 
						 builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
						   public void onClick(DialogInterface dialog, int which) {
							   
							   dialog.dismiss();
							   setFax_Id(getItem(position).get(3));
							   System.out.println(getFax_Id());
							   deleteFax(getFax_Id());
							  
						   }
						 });
						 builder.setNegativeButton("View", new DialogInterface.OnClickListener() {
						   public void onClick(DialogInterface dialog, int which) {
						     dialog.dismiss();
						   }
						 });
						 
						 builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
							   public void onClick(DialogInterface dialog, int which) {
								   
								   dialog.dismiss();
								   
							   }
							 });
						 
						 
						 AlertDialog alert = builder.create();
						 alert.show();
					 }
					});
				
			
		} else { // Reuse/Overwrite the View passed
			
			btv = (IconifiedTextView) convertView;
			btv.setText(mItems.get(position));
			btv.setBackgroundColor(Color.WHITE);
			btv.setScrollContainer(true);

		}
		return btv;
	}

	
	public void deleteFax(String selected_fax_id)
	{

	fax_Id=selected_fax_id;
	progressDialog = ProgressDialog.show(mContext,Constants.DIALOG_WAIT_MSG,Constants.CONTACT_DELETION_MSG, true,false);
	Thread thread = new Thread(this);
	thread.start(); 
	deleteHandlerMethod();

	}

public void run() {
	
	
	setMapValues();
	Requestor requestorForXMLParsing= new Requestor();
	arrayOfObjectOfParsedData=requestorForXMLParsing.requestData('d',parameterValues);
	handler.sendEmptyMessage(0);		
}



public void deleteHandlerMethod()
{
	handler = new Handler() {

		public void handleMessage(Message msg) {
			progressDialog.dismiss();

			System.out.println("Handler");
			 if(((ParsedDeleteDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_SUCCESS_CODE)
				 {
					 CurrentTab currenttab = new CurrentTab(mContext,mtab);
				 }
				 if(((ParsedDeleteDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_FAILURE_CODE)
 			 {
 				 Toast.makeText(mContext,Constants.LOGGED_IN_FAILED_MSG, Toast.LENGTH_SHORT).show();
 				CurrentTab currenttab = new CurrentTab(mContext,mtab);
 	
 			 }
			 
		}
		
	};

}



void setMapValues() {

	loadPreference(); 
	parameterValues = new HashMap();
	parameterValues.put(Constants.URL_ACTION, Constants.DELETE_FAX_ACTION);
	parameterValues.put(Constants.URL_USERNAME,userName_shared_prefernece);
	parameterValues.put(Constants.URL_PASSWORD,passWord_shared_prefernece);
	parameterValues.put(Constants.FAX_ID,fax_Id );
}



protected void loadPreference()
{
	int mode = Activity.MODE_PRIVATE;
	
	
	SharedPreferences mySharedPreferences = mContext.getSharedPreferences("preferences", mode);
	userName_shared_prefernece= mySharedPreferences.getString("USER_NAME","UserName");
	System.out.println("Delete test"+userName_shared_prefernece);
	passWord_shared_prefernece = mySharedPreferences.getString("PASS_WORD","Password");
	System.out.println("Delete test"+passWord_shared_prefernece);

	/*userName_shared_prefernece="mah.btech@gmail.com";
	passWord_shared_prefernece="e54vwjuf";*/
}
	
	
}
